import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:ty_smart_watch/ble/ble_util.dart';
import 'package:ty_smart_watch/ble/connected_device_controller.dart';
import 'package:ty_smart_watch/ble/protocol_data_util.dart';
import 'package:ty_smart_watch/common/function_id.dart';
import 'package:ty_smart_watch/common/viewmodel.dart';
import 'package:ty_smart_watch/page/smartlife/controller/body_grid_controller.dart';
import 'package:ty_smart_watch/route/routes.dart';
import 'package:ty_smart_watch/utils/Log.dart';
import 'package:ty_smart_watch/utils/grid_drag_view.dart';
import '../../common/images_url.dart';
import 'controller/today_step_controller.dart';

///智能生活页面，今日步数栏

class TodayOverview extends StatelessWidget {
  const TodayOverview({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(StepController());
    return Container(
      // alignment: Alignment.center,
      padding: const EdgeInsets.all(10),
      margin: const EdgeInsets.fromLTRB(10, 16, 10, 0),
      decoration: const BoxDecoration(
        //圆角半径
        borderRadius: BorderRadius.all(Radius.circular(12.0)),
        boxShadow: [
          BoxShadow(color: Colors.grey, offset: Offset(0, 0), blurRadius: 8, spreadRadius: 0),
        ],
        // 线性渐变，分别设置渐变的颜色，起始点
        gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [Colors.white, Color(0xFFE2F6ED)]),
      ),
      child: const Column(
        children: [
          //今日步数标题
          TodayStepTitle(),
          SizedBox(height: 4),
          //今日步数数据
          TodayStep(),
          SizedBox(height: 6),
          //今日其他数据
          TodayOther(),
          SizedBox(height: 10),
        ],
      ),
    );
  }
}

///今日数据标题
class TodayStepTitle extends StatelessWidget {
  const TodayStepTitle({super.key});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Row(children: [
        Image.asset(TODAY_STEP, width: 36, height: 36),
        const SizedBox(width: 6),
        const Text("今日步数", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold))
      ]),
      onTap: () {
        // Get.toNamed(Routes.obdPage);
        // Get.to(GridDragView());
        //更新列表
        // controller.updateDate(HEART, "${Random().nextInt(100)}");

        // BleUtil.connectToAdvertisingDevice("36:ED:EC:E5:E4:A3", [], {}, () {
        //     ViewModel.getInitInfo("36:ED:EC:E5:E4:A3");
        // });

        // SmartDialog.showLoading(msg: "连接中");

        // final StepController stepController = Get.find();
        // var percent = double.parse(stepController.currStep.value) / 2000;
        // Log.d("percent: $percent");

        // DeviceController controller = Get.find();
        // var len = controller.connectDeviceMap.value.length;
        // Log.d("绑定设备的数量：$len");

        // var name0 = controller.connectDeviceMap.keys.toList()[0];
        // Log.d("0：$name0");

        // print("点击了头像");
        // final StepController stepController = Get.find();
        // stepController.updateCal("321");
        // stepController.updateTargetStep("1000");
      },
    );
  }
}

///今日步数数据
class TodayStep extends StatefulWidget {
  const TodayStep({super.key});

  @override
  State<StatefulWidget> createState() {
    return _TodayStepState();
  }
}

class _TodayStepState extends State<TodayStep> {
  final StepController stepController = Get.find();

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Obx(
          () => Text(stepController.currStep.value,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
        ),
        const Text(" / ", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
        Obx(
          () => Text(stepController.targetStep.value,
              style: const TextStyle(fontSize: 16, height: 2)),
        ),
        const Text("  步", style: TextStyle(fontSize: 16, height: 2)),
      ],
    );
  }
}

///今日其他数据
class TodayOther extends StatelessWidget {
  const TodayOther({super.key});

  @override
  Widget build(BuildContext context) {
    final StepController stepController = Get.find();
    return Row(
      children: [
        TodayOtherItem(iconUrl: TODAY_CAL, content: stepController.cal, unit: "千卡"),
        TodayOtherItem(iconUrl: TODAY_DISTANCE, content: stepController.distance, unit: "公里"),
        TodayOtherItem(iconUrl: TODAY_HEART, content: stepController.heart, unit: "次/分"),
      ],
    );
  }
}

///今日其他数据子项
class TodayOtherItem extends StatelessWidget {
  // final StepController stepController = Get.find();
  final String iconUrl;
  final RxString content;
  final String unit;

  const TodayOtherItem(
      {super.key, required this.iconUrl, required this.content, required this.unit});

  @override
  Widget build(BuildContext context) {
    return Expanded(
        flex: 1,
        child: Row(
          children: [
            Image.asset(iconUrl, width: 20, height: 20),
            Container(
              margin: EdgeInsets.symmetric(horizontal: 4),
              child: Obx(() => Text(
                    content.value,
                    style: const TextStyle(fontSize: 12),
                  )),
            ),
            Text(unit)
          ],
        ));
  }
}
